package co.fitstart.mobile.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.BusinessException;
import com.demo2do.core.persistence.GenericDaoSupport;

import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.agent.Agent;
import co.fitstart.entity.user.agent.AgentDiscount;
import co.fitstart.entity.user.agent.AgentRecord;
import co.fitstart.entity.user.agent.OfflineAgent;
import co.fitstart.entity.user.agent.RecordType;
import co.fitstart.mobile.dao.support.AgentDaoSupport;
import co.fitstart.mobile.service.UserAgentService;

/**
 * 
 * @author mohanlan
 *
 */
@Service("userAgentService")
public class UserAgentServiceImpl implements UserAgentService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    @Autowired
    private AgentDaoSupport agentDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#get(co.fitstart.entity.user.User)
     */
    public Agent get(User user) {
        return agentDaoSupport.loadAgent(user);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#editAccount(co.fitstart.entity.user.agent.Agent, java.lang.String, java.lang.String)
     */
    public void editAccount(Agent agent, String account, String realName) {
        genericDaoSupport.update(agent.editAccount(account,realName));
    }
    
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#listDiscount(co.fitstart.entity.user.agent.Agent)
     */
    public List<AgentDiscount> listDiscount(Agent agent) {
        return agentDaoSupport.listAgentDiscount(agent);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#loadAgentDiscount(java.lang.String)
     */
    public AgentDiscount loadAgentDiscount(String code) {
        AgentDiscount agentDiscount = agentDaoSupport.loadDiscount(code);
        
        if(agentDiscount == null) {
            throw new BusinessException("折扣券码有误，请重新填写");
        } else if (agentDiscount.isDisabled()) {
            throw new BusinessException("折扣券码已失效");
        }
        
        
        return agentDiscount;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#listAgentRecord(co.fitstart.entity.user.agent.Agent)
     */
    public List<AgentRecord> listAgentRecord(Agent agent) {
        return agentDaoSupport.listAgentRecord(agent);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#withdraw(co.fitstart.entity.user.agent.Agent, java.math.BigDecimal)
     */
    public void withdraw(Agent agent, BigDecimal offer) {
        
        int offerFen = offer.multiply(new BigDecimal(100)).intValue();
        // save agent record
        AgentRecord agentRecord = new AgentRecord(agent, RecordType.WITHDRAWING, offerFen);
        genericDaoSupport.save(agentRecord);
        
        // update agent withdrawl price
        genericDaoSupport.update(agent.withdraw(offerFen));
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.mobile.service.UserAgentService#listOfflineAgent(co.fitstart.entity.user.Student)
     */
    public List<OfflineAgent> listOfflineAgent(Student student) {
        return agentDaoSupport.listOfflineAgent(student);
    }
}
